/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedom.imarket.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author edoardo
 */
@Controller
public class EntityController {

//    @Autowired
    private Map<String, AbstractEntityController> controllers ;//= new Hashtable<String, AbstractEntityController>();

//    @PostConstruct
//    public void init() {
//        controllers.put("comune", new ComuneController().init());
//        controllers.put("societa", new SocietaController().init());
//    }

    @RequestMapping(value = "/**/view/{grid}/List.js")
    public String loadListJs(@PathVariable String grid, HttpServletRequest request, HttpServletResponse response) throws JSONException, IOException {
        System.out.println("Controllers: "+controllers);
        return controllers.get(grid).loadListJs(request, response);
    }
    
    @RequestMapping(value="/**/init{grid}.js")
    public void loadApplication(@PathVariable String grid,HttpServletRequest request,HttpServletResponse response){
    	controllers.get(grid).initComponent(request,response);
    }

    @RequestMapping(value = "/**/view/{grid}/Edit.js")
    public String loadEditJs(@PathVariable String grid, HttpServletRequest request, HttpServletResponse response) throws JSONException, IOException {
        return controllers.get(grid).loadEdit(request, response);
    }

    @RequestMapping(value = "/**/store/{name}.js")
    public String loadAllSocieta(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws IOException, JSONException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        System.out.println("loadAllSocieta::Value of name: "+name);
        return controllers.get(name.toLowerCase()).loadGrid(request, response);
    }

    @RequestMapping(value = "/**/model/{name}.js")
    public String loadModel(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("loadAllSocieta::Value of name: "+name);
        return controllers.get(name.toLowerCase()).loadModel(request, response);
    }
    
        @RequestMapping(value = "/**/controller/{name}.js")
    public String loadController(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws IOException {
        return controllers.get(name.toLowerCase()).loadController(request, response);
    }

}
